package com.xm.adv;

import android.app.Presentation;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.VideoView;

import com.xm.adv.Ctr.DrawCtr;

import java.util.ArrayList;
import java.util.List;

import xinya.com.baselibrary.bean.program.SceneBean;
import xinya.com.baselibrary.utils.LogUtil;

/**
 * Created by zhangjiebo on 2019/6/2.
 *
 * @author ZhangJieBo
 */

public class DifferentDislay extends Presentation {
    RelativeLayout viewDraw01;
    RelativeLayout viewDraw02;
    private List<SceneBean> list02 = new ArrayList<>();
    int positionX = 0;
    int time = 15000;
    private Runnable runnable;
    private List<VideoView> videoViewList01;
    private List<VideoView> videoViewList02;

//    private float zoom= 0.3164f;
    private float zoom= 1.78f;
    public DifferentDislay(Context outerContext, Display display) {

        super(outerContext, display);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.diffrentdisplay_basket);
        viewDraw01 = findViewById(R.id.viewDraw01);
        viewDraw02 = findViewById(R.id.viewDraw02);

    }

    /**
     * 初始化
     * @param list02
     */
    public void setData(List<SceneBean> list02) {
        this.list02 = list02;
        setDealy(0);
    }

    /**
     * 添加节目
     */
    public void addProgram(SceneBean scene){
        list02.add(scene);
    }

    //延时翻页
    private void setDealy(int position) {
        positionX = position;
        if (runnable != null) {
            viewDraw01.removeCallbacks(runnable);
        }
        add();
        runnable = () -> {
            if (list02.size() > 1) {
                if (viewDraw01.getVisibility() == View.VISIBLE) {
                    for (int i = 0; i < videoViewList02.size(); i++) {
                        videoViewList02.get(i).start();
                    }
                    viewDraw01.setVisibility(View.INVISIBLE);
                    viewDraw02.setVisibility(View.VISIBLE);
                    if (list02.size() > 1) {
                        LogUtil.LogShitou("MainActivity--setDealy", "1111");
                        int index = positionX;
                        index++;
                        if (index == list02.size()) {
                            index = 0;
                        }
                        videoViewList01 = DrawCtr.draw(getContext(), viewDraw01, list02.get(index),zoom,1);
                    }
                } else {
                    for (int i = 0; i < videoViewList01.size(); i++) {
                        videoViewList01.get(i).start();
                    }
                    viewDraw01.setVisibility(View.VISIBLE);
                    viewDraw02.setVisibility(View.INVISIBLE);
                    if (list02.size() > 1) {
                        LogUtil.LogShitou("MainActivity--setDealy", "2222");
                        int index = positionX;
                        index++;
                        if (index == list02.size()) {
                            index = 0;
                        }
                        videoViewList02 = DrawCtr.draw(getContext(), viewDraw02, list02.get(index),zoom,1);
                    }
                }
                SceneBean sceneBean = list02.get(positionX);
                String duration = sceneBean.getDuration();
                if (!TextUtils.isEmpty(duration)) {
                    time = Integer.parseInt(duration);
                }
                positionX++;
                if (positionX == list02.size()) {
                    positionX = 0;
                }
            }
            viewDraw01.postDelayed(runnable, time);
        };
        viewDraw01.postDelayed(runnable, time);
    }

    private void add() {
        viewDraw01.setVisibility(View.VISIBLE);
        viewDraw02.setVisibility(View.INVISIBLE);
        LogUtil.LogShitou("MainActivity--add", "" + list02.get(positionX).getId());
        videoViewList01 = DrawCtr.draw(getContext(), viewDraw01, list02.get(positionX),zoom,1);
        SceneBean sceneBean01 = list02.get(positionX);
        String duration = sceneBean01.getDuration();
        if (!TextUtils.isEmpty(duration)) {
            time = Integer.parseInt(duration);
        }
        positionX++;
        if (positionX == list02.size()) {
            positionX = 0;
        }
        videoViewList02 = DrawCtr.draw(getContext(), viewDraw02, list02.get(positionX),zoom,1);
    }

    /**
     * 清空副屏内容
     */
    public void clear() {
        for (int i = 0; i < videoViewList01.size(); i++) {
            videoViewList01.get(i).stopPlayback();
        }
        for (int i = 0; i < videoViewList02.size(); i++) {
            videoViewList01.get(i).stopPlayback();
        }
        viewDraw01.setVisibility(View.GONE);
        viewDraw02.setVisibility(View.GONE);
    }

}


//    DisplayManager mDisplayManager;//屏幕管理类
//
//    Display[] displays;//屏幕数组
//
//        mDisplayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
//
//                displays = mDisplayManager.getDisplays();
//
//                Log.e("MainActivity--onCreate", "" + displays.length);
//
//                DifferentDislay mPresentation = new DifferentDislay(this, displays[1]);//displays[1]是副屏
//
//                mPresentation.getWindow().setType(
//
//                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
//
//                mPresentation.show();